<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\Filesystem;

class Index extends Common
{
    /**
     * 商品列表
     * @return string|\think\response\Redirect
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        // 获取session中的数据
        $login = Session::get('account');
        if(!Session::has('account')){
            return redirect('/admin/login');  //没有登录就跳转到登录页面
        }
        // 获取左侧菜单列表
        $left = Db::table('shop_menu')->where('fid',0)->select()->toArray();
        foreach($left as &$left_v){
            $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
        }
        // 根据关键字搜索商品
        $param = Request::param();  //取出所有参数
        if(isset($param['keyword'])){   //根据关键字搜索
            $keyword = $param['keyword'];
            $list = Db::table('shop_goods')->whereLike('title',"%$keyword%")->paginate(10);  //分页
        }else{      //显示所有数据
            $list = Db::table('shop_goods')->paginate(10);   //分页
        }
        $right = $list;
        foreach($right as &$right_v){
            $right_v['cat'] = Db::table('shop_cat')->where('id',$right_v['cat'])->value('name');
        }
        MyViewAssign([
            'login' => $login,
            'left' => $left,
            'right' => $right,
            'list'  =>$list     //分页数据
        ]);
        return MyView();
    }

    /**
     * 商品添加
     * @return string|\think\response\Redirect|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if(Request::method() == 'POST'){    //点击保存
            $all = Request::param();
            $all['add_time'] = time();
            $file = request()->file('image');
            $savename = Filesystem::disk('public')->putFile( '/', $file);
            $all['image'] = $savename;
            $all['add_time'] = time();
            $insert = Db::table('shop_goods')->insert($all);
            if($insert){  //成功
                echo "<script>alert('添加成功');window.location.href='/admin/index/index'</script>";
                exit;
            }else{  //失败
                echo "<script>alert('添加失败');window.location.href='/admin/index/add'</script>";
                exit;
            }
        }else{  //第一次点击添加时
            $login = Session::get('account');
            if(!Session::has('account')){
                return redirect('/admin/login');  //没有登录就跳转到登录页面
            }
            $cat = Db::table('shop_cat')->where('status',1)->select()->toArray();
            $cat = $this->getTree($cat);
//            var_dump($cat);exit;
            $left = Db::table('shop_menu')->where('fid',0)->select()->toArray();
            foreach($left as &$left_v){
                $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
            }
            MyViewAssign([
                'login' =>$login,
                'cat' => $cat,
                'left' =>$left
            ]);
            return MyView();
        }
    }

    /**
     * 商品编辑
     * @return string|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        if(Request::method() == 'POST'){   //保存执行修改操作
            $all = Request::param();
            $all['add_time'] = time();
            $update = Db::table('shop_goods')->where('id',$all['id'])->update($all);
            if($update){    //成功
                echo json_encode(['code'=>0,'msg'=>'修改成功']);
                exit;
            }else{  //失败
                echo json_encode(['code'=>1,'msg'=>'修改失败']);
                exit;
            }
        }else{      //查询需要修改记录
            $id = Request::param('id');
            $shop = Db::table('shop_goods')->where('id',$id)->find();
            $cat = Db::table('shop_cat')->where('status',1)->select();
            $cat = $this->getTree($cat);
            MyViewAssign([
                'shop' => $shop,
                'cat' => $cat
            ]);
            return MyView();
        }
    }

    /**
     * 商品删除
     * @throws \think\db\exception\DbException
     */
    public function del()
    {
        $id = Request::param('id');
        $delete = Db::table('shop_goods')->where('id',$id)->delete();
        if($delete){    //成功
            echo json_encode(['code'=>0,'msg'=>'删除成功']);
            exit;
        }else{  //失败
            echo json_encode(['code'=>1,'msg'=>'删除失败']);
            exit;
        }
    }

    /**
     * 用户列表
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function userList()
    {
        $title = "电子商城";
        $login = Session::get('account');
        // 获取左侧菜单列表
        $left = Db::table('shop_menu')->where('fid',0)->select()->toArray();
        foreach($left as &$left_v){
            $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
        }
        $users = Db::table("shop_user")->select()->toArray();
        foreach ($users as &$v){
            $v["add_time"] = date("Y-m-d H:i:s",$v["add_time"]);
            $v["up_time"] = date("Y-m-d H:i:s",$v["up_time"]);
        }
        MyViewAssign([
            "title" => $title,
            "login" => $login,
            "left" => $left,
            "users" => $users
        ]);
        return MyView();
    }

    /**
     * 购物车列表
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cartList()
    {
        $title = "电子商城";
        $login = Session::get('account');
        // 获取左侧菜单列表
        $left = Db::table('shop_menu')->where('fid',0)->select()->toArray();
        foreach($left as &$left_v){
            $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
        }
        $carts = Db::table("shop_cart")
                ->alias("c")
                ->leftJoin(["shop_user" => "u"],"c.user_id = u.id")
                ->leftJoin(["shop_goods" => "g"],"c.goods_id = g.id")
                ->field(["c.id","u.nickname","g.title","c.num","c.add_time","c.up_time"])
                ->select();
        foreach ($carts as &$v){
            $v["add_time"] = date("Y-m-d H:i:s",$v["add_time"]);
            $v["up_time"] = date("Y-m-d H:i:s",$v["up_time"]);
        }
        MyViewAssign([
            "title" => $title,
            "login" => $login,
            "left" => $left,
            "carts" => $carts
        ]);
        return MyView();
    }

    /**
     * 地址列表
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addressList()
    {
        $title = "电子商城";
        $login = Session::get('account');
        // 获取左侧菜单列表
        $left = Db::table('shop_menu')->where('fid',0)->select()->toArray();
        foreach($left as &$left_v){
            $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
        }
        $addressList = Db::table("shop_address")->select()->toArray();
        foreach ($addressList as &$v){
            $v["is_default"] = $v["is_default"] == 1 ? "是" : "否";
            $v["is_delete"] = $v["is_delete"] == 1 ? "正常" : "删除";
            $v["add_time"] = date("Y-m-d H:i:s",$v["add_time"]);
            $v["up_time"] = date("Y-m-d H:i:s",$v["up_time"]);
        }
        MyViewAssign([
            "title" => $title,
            "login" => $login,
            "left" => $left,
            "addressList" => $addressList
        ]);
        return MyView();
    }

    /**
     * 订单列表
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderList()
    {
        $title = "电子商城";
        $login = Session::get('account');
        // 获取左侧菜单列表
        $left = Db::table('shop_menu')->where('fid',0)->select()->toArray();
        foreach($left as &$left_v){
            $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
        }
        $orders = Db::table("shop_order")
            ->alias("o")
            ->leftJoin(["shop_user" => "u"],"o.user_id = u.id")
            ->leftJoin(["shop_goods" => "g"],"o.goods_id = g.id")
            ->field(["o.*","u.nickname","g.title"])
            ->select()
            ->toArray();
        foreach ($orders as &$v){
            $v["pay_type"] = $this->payType[$v["pay_type"]];
            $v["pay_status"] = $this->pay_status[$v["pay_status"]];
            $v["status"] = $this->status[$v["status"]];
            $v["add_time"] = date("Y-m-d H:i:s",$v["add_time"]);
            $v["up_time"] = date("Y-m-d H:i:s",$v["up_time"]);
        }
        MyViewAssign([
            "title" => $title,
            "login" => $login,
            "left" => $left,
            "orders" => $orders
        ]);
        return MyView();
    }
}